# coding=utf-8
import sys
import re

from flask import Flask
from flask import render_template
from flask import Blueprint

app = Flask(__name__)
blue = Blueprint("./",__name__,template_folder="",static_folder="",static_url_path="")

@blue.route("/")
def DataVisualization():
    data = GetData()
    return render_template("data.html",title="cnn_lstm",data=data)

def GetData():
    data = {"legend":[],"x":[],"y":[],"series":[]}
    for filename in name_of_data_files:
        f = open(filename,"r")
        data["legend"].append("loss")
        data["series"].append({"name":"loss","type":"line","data":[]})
        for line in f.readlines():
            if line.startswith("time"):
                data["x"].append(int(re.findall("(?<=epoch:).*?(?=,)",line)[0]))
                data["series"][0]["data"].append(float(re.findall("(?<=loss:).*",line)[0]))
        f.close()
    return data


usage = "usage:\n\tpython main.py nameofdatafile\n"
name_of_data_files = ""
if __name__ == "__main__":
    if len(sys.argv) <= 1:
        print usage 
    else:
        name_of_data_files = []
        for i in range(1,len(sys.argv)):
            name_of_data_files.append(sys.argv[i])
        app.register_blueprint(blue)
        app.run(debug=True)

